﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14

Window {
    title: mltype==="0"?"新建":"编辑"
    width: 800*dp
    height: 650*dp
    property string mltype: "0"
    property string hcna: ""
    property string indextype: ""
    property var tablemodeldata: []
    property var hcmod: []
    property int zsqsum: 6
    property bool zsfl: false
    property bool newZsqButtonClicked: false
    signal updata()
    Toast{
        id: toast
        y: 70*dp
        x: parent.width / 2 - 150*dp
        width: 350*dp
        z: 1

    }
    Connections{
        target: CSignal
        onSendSyringRunMsg:{
            if(status==="0"){
                if(serial==="7"){
                    toast.show("转盘执行完成",0)
//                    tzbt.enabled=true
//                    tzbt.text_color="black"
//                    tzbt.button_color="#FF9478"
//                    tzbt.button_border="transparent"
//                    tzbt.button_color_press="#FF9400"
                }
                else if(serial!="8")
                {
                    toast.show(serial+"号注射器执行完成",0)
                    fwbt.enabled=true
                }
            }
            else if(status==="1"){
                if(serial==="7"){
                    toast.show("转盘正在执行中,请耐心等待",1)
                }
                else if(serial!="8")
                {
                    toast.show(serial+"号注射器正在执行中,请耐心等待",1)
                }
            }
        }
        onSendResetMsg:{
            if(status==="0"){
                if(serial==="7"){
                    toast.show("转盘复位完成",0)
                }
                else{
                    toast.show(serial+"号注射器复位完成",0)
                    tzbt.enabled=true
                    zxbt.enabled=true
                }
            }
            else if(status==="1"){
                if(serial==="7"){
                    toast.show("转盘正在复位,请耐心等待",1)
                }
                else if(serial==="0")
                {
                    toast.show("复位动作暂未完成,请耐心等待",1)
                }
                else{
                    toast.show(serial+"号注射器复位未完成,请耐心等待",1)
                }
            }
        }
    }
    onVisibleChanged: {
        console.log(zsqsum)
        fypage.pageCount=zsqsum
        fypage.pagerCount=zsqsum
        fypage.page=1
        mc.setText(tablemodeldata[0][0])
        lx.setText(tablemodeldata[0][1])
        nj.setText(tablemodeldata[0][2])
        njgc.setText(tablemodeldata[0][3])
        qswz.setText(tablemodeldata[0][4])
        zsqsd.setText(tablemodeldata[0][5])
        zsqdw.setText(tablemodeldata[0][6])
        jsb.setText(tablemodeldata[0][7])
        dc.setText(tablemodeldata[0][8])
        qb.setText(tablemodeldata[0][9])
        bs.setText(tablemodeldata[0][10])
        zbs.setText(tablemodeldata[0][11])
        if(zsqdw.getText()==="mL/Min"){
            qswz.title="起始位置(mL)"
        }else{
            qswz.title="起始位置(mm)"
        }

    }
    BoxWarning{
        id:hcts
        box_text: "耗材名称不可重复！"
        bt1vis:false
    }
    CustomButton{
        id:fwbt
        title: "复位原点"
        defaultMode1: true
        width: 120*dp
        x:parent.width-530*dp
        y:20*dp
        onReleased: {
            CMaintenance.reset("0",fypage.page.toString(),"")
            tzbt.enabled=false
            zxbt.enabled=false
        }
    }
    CustomButton{
        id:zxbt
        title: "执行"
        defaultMode1: true
        width: 120*dp
        x:parent.width-400*dp
        y:20*dp
        onReleased: {
            if(qswz.getText()===""||lx.getText()===""||nj.getText()===""||zsqsd.getText()===""){
                toast.show("请确认参数是否填写完整！",1)
            }else{
                var data =CMaintenance.checkSyringParameter(lx.getText(),nj.getText(),qswz.getText(),zsqsd.getText(),zsqdw.getText(),jsb.getText(),dc.getText(),qb.getText())
                if(data===""){
                    CMaintenance.runningSyring(hclx.getText(),fypage.page.toString(),bs.getText(),zbs.getText())
                    fwbt.enabled=false
                }else{
                    toast.show(data,1)
                }
            }
        }
    }
    CustomButton{
        title: "保存"
        defaultMode1: true
        width: 120*dp
        x:parent.width-270*dp
        y:20*dp
        CustomTooltip{
            visible: parent.isHovered
            text: "保存数据到数据库"
        }
        onReleased: {
            if(tablemodeldata[fypage.page-1][0]===""||tablemodeldata[fypage.page-1][1]===""||tablemodeldata[fypage.page-1][2]===""||tablemodeldata[fypage.page-1][4]===""||tablemodeldata[fypage.page-1][5]===""){
                toast.show("请确认参数是否填写完整！",1)
                return
            }
            CMaintenance.saveSyring(mltype,indextype,hclx.getText(),tablemodeldata)
            toast.show("保存成功!",0)
        }
    }
    CustomButton{
        id:tzbt
        y:20*dp
        x:parent.width-140*dp
        text_color: "black"
        button_color: "#FF9478"
        button_border : "transparent"
        button_color_press : "#FF9400"
        title: "停止"
        onReleased: {
            CMaintenance.singleStop(fypage.page)
        }
    }
    Rectangle{
        x:20*dp
        y:80*dp
        width: parent.width-40*dp
        height: parent.height-100*dp
        radius: 12
        border.color: "#B8B7B5"

        CustomComboBox{
            id:hclx
            y:5*dp
            x:10*dp
            title: "耗材名称"
            titleWidth: 80*dp
            editWidth: 120*dp
            editDefaultText: hcna
            model: hcmod
            enabled: mltype==="0" ? true : false
            onCurrentText: {
                zsqsum=CMaintenance.getSyringNum(hclx.getText())
                fypage.pageCount=zsqsum
                fypage.pagerCount=zsqsum
                fypage.page=1
                var rows = zsqsum
                // 创建一个空的二维数组
                var twoDArray = [];
                // 使用嵌套循环初始化二维数组
                for (var i = 0; i < rows; i++) {
                    // 为每一行创建一个新的数组
                    twoDArray[i] = ["",CExeConfig.getzsqdef()[0],"","","","","mL/Min","1:1","0.6096",
                            "3200","",""];
                }
                mc.setText(tablemodeldata[0][0])
                lx.setText(tablemodeldata[0][1])
                nj.setText(tablemodeldata[0][2])
                njgc.setText(tablemodeldata[0][3])
                qswz.setText(tablemodeldata[0][4])
                zsqsd.setText(tablemodeldata[0][5])
                zsqdw.setText(tablemodeldata[0][6])
                jsb.setText(tablemodeldata[0][7])
                dc.setText(tablemodeldata[0][8])
                qb.setText(tablemodeldata[0][9])
                bs.setText(tablemodeldata[0][10])
                zbs.setText(tablemodeldata[0][11])
                if(zsqdw.getText()==="mL/Min"){
                    qswz.title="起始位置(mL)"
                }else{
                    qswz.title="起始位置(mm)"
                }
            }
        }

        Text {
            x:240*dp
            y:15*dp
            text: qsTr("编号")
            font.pixelSize: config.pointsize + 2*dp
        }

//         分页器-扩展样式
        CustomPagination{
            id:fypage
            x:280*dp
            y:12*dp
            width: parent.width-60*dp
            height: 30*dp
            defaultMode1: true      // 默认样式1
            pageCount: zsqsum          // 总页数
            pagerCount: zsqsum           // 展示的最大页数，多余的页数将用省略号显示
            page: 1                 // 可直接通过page设置当前页
            goToPage: false          // 页面跳转
            onPageChanged: {        // 监听事件
                mc.setText(tablemodeldata[page-1][0])
                lx.setText(tablemodeldata[page-1][1])
                nj.setText(tablemodeldata[page-1][2])
                njgc.setText(tablemodeldata[page-1][3])
                qswz.setText(tablemodeldata[page-1][4])
                zsqsd.setText(tablemodeldata[page-1][5])
                zsqdw.setText(tablemodeldata[page-1][6])
                jsb.setText(tablemodeldata[page-1][7])
                dc.setText(tablemodeldata[page-1][8])
                qb.setText(tablemodeldata[page-1][9])
                bs.setText(tablemodeldata[page-1][10])
                zbs.setText(tablemodeldata[page-1][11])
                if(zsqdw.getText()==="mL/Min"){
                    qswz.title="起始位置(mL)"
                }else{
                    qswz.title="起始位置(mm)"
                }
            }
        }
        Rectangle{
            width: parent.width
            height: 1*dp
            x:0*dp
            y:50*dp
            color: "#B8B7B5"
        }
        CustomInputText{
            id:mc
            title: "名称"
            width: 260*dp
            x:40*dp
            y:80*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            onTextChanged: {
                tablemodeldata[fypage.page-1][0]=mc.getText()
            }
        }
        CustomComboBox{
            id:lx
            title:qsTr("类型")
            x:420*dp
            y:80*dp
            width: 260*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            model: CExeConfig.getzsqdef()
            onCurrentText: {
                tablemodeldata[fypage.page-1][1]=lx.getText()
            }
        }
        CustomInputText{
            id:nj
            title: qsTr("<font color='red'></font>内径(mm)")
            width: 260*dp
            x:40*dp
            y:140*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            douflag: true
            onTextChanged: {
                tablemodeldata[fypage.page-1][2]=nj.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomInputText{
            id:njgc
            title: qsTr("<font color='red'></font>内径公差(mm)")
            width: 260*dp
            x:420*dp
            y:140*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            douflag: true
            onTextChanged: {
                tablemodeldata[fypage.page-1][3]=njgc.getText()
            }
        }
        CustomInputText{
            id:qswz
            title: qsTr("<font color='red'></font>起始位置(mL)")
            width: 260*dp
            x:40*dp
            y:200*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            douflag: true
            onTextChanged: {
                tablemodeldata[fypage.page-1][4]=qswz.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomInputText{
            id:zsqsd
            x:420*dp
            y:200*dp
            title: qsTr("<font color='red'></font>电机速度")
            width: 200*dp
            titleWidth: 100*dp
            douflag: true
            editWidth: 100*dp
            onTextChanged: {
                tablemodeldata[fypage.page-1][5]=zsqsd.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomComboBox{
            id:zsqdw
            x:630*dp
            y:200*dp
            width: 100*dp
            editWidth: 100*dp
            titleWidth: 0*dp
            model: ["mL/Min","mm/s"]
            onCurrentText: {
                tablemodeldata[fypage.page-1][6]=zsqdw.getText()
                if(text==="mL/Min"){
                    qswz.title="起始位置(mL)"
                }else{
                    qswz.title="起始位置(mm)"
                }

                if(zsqsd.getText()===""){
                    return
                }
                zsqsd.setText(GetMessageInterface.unitConversion(zsqsd.getText(),nj.getText(),zsqdw.getText()))
            }
        }
        Rectangle{
            width: parent.width-60*dp
            height: 1*dp
            x:30*dp
            y:270*dp
            color: "#B8B7B5"
        }
        Text {
            x:20*dp
            y:280*dp
            text: qsTr("电机配置")
            font.bold: true
            font.pixelSize: config.pointsize + 2*dp
        }
        CustomComboBox{
            id:jsb
            title: "减速比"
            width: 260*dp
            x:40*dp
            y:340*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            model:["1:1","1:2","1:3","1:4","1:5","1:6","1:7","1:8","1:9","1:10",
                "1:11","1:12","1:13","1:14","1:15","1:16","1:17","1:18","1:19","1:20"]
            onCurrentText: {
                tablemodeldata[fypage.page-1][7]=jsb.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomInputText{
            id:dc
            title: qsTr("<font color='red'></font>导程(圈/毫米)")
            width: 260*dp
            x:420*dp
            y:340*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            douflag: true
            onTextChanged: {
                tablemodeldata[fypage.page-1][8]=dc.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomComboBox{
            id:qb
            title: "圈步"
            width: 260*dp
            x:40*dp
            y:400*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            model:["3200"]
            onCurrentText: {
                tablemodeldata[fypage.page-1][9]=qb.getText()
                if(zsqdw.getText()==="mL/Min"){
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),nj.getText(),jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }else{
                    bs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[0])
                    zbs.setText(CMaintenance.calculateSteps(zsqsd.getText(),"",jsb.getText(),dc.getText(),qb.getText(),qswz.getText())[1])
                }
            }
        }
        CustomInputText{
            id:bs
            title: "步数(步/秒)"
            width: 260*dp
            editable: false
            x:420*dp
            y:400*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            onTextChanged: {
                tablemodeldata[fypage.page-1][10]=bs.getText()
            }
        }
        CustomInputText{
            id:zbs
            title: "总步数"
            width: 260*dp
            editable: false
            x:40*dp
            y:460*dp
            titleWidth: 100*dp
            editWidth: 160*dp
            onTextChanged: {
                tablemodeldata[fypage.page-1][11]=zbs.getText()
            }
        }
    }
}
